/////////////////////////////////////////////////////////////
// TITLE: 			Panelify HD Splits, Construct Covariates
//
// DESCRIPTION:
// Generate measures of flows
/////////////////////////////////////////////////////////////

local part = `1'

// Quarterly frequency
use "$reconstruct/intermediate/HD_part_`part'_panel_intermediate", clear
gen month=month(dofm(date_m))
gen date_q=qofd(dofm(date_m))
bysort date_q id : egen sharechange_inqtr = total(sharechange)
keep if mod(month,3)==0
drop month
bysort id : egen sec_in_qtrly_sample = total(marketvalue)
drop if sec_in_qtrly == 0
drop sec_in_qtrly
format date_q %tq
tsset, clear
sort id date_q
tsset id date_q 

// Standardize the way that flows are reported baetween funds
bysort MasterPortfolioId date_m: egen anyreport=total(marketvalueusd)
* Have to tsset again after any other sort operation.
tsset id date_q
drop if anyreport==0
drop anyreport
gen matured_or_soldout = 1 if ((missing(numberofshare) | numberofshare==0) & (missing(marketvalue) | marketvalue==0) & (!missing(L.numberofshare) & L.numberofshare!=0) & (!missing(L.marketvalue) & L.marketvalue!=0))
drop if missing(numberofshare) & missing(marketvalue) & missing(sharechange) & missing(matured_or_soldout)
replace sharechange = sharechange_inqtr
drop sharechange_inqtr
replace numberofshare = 0 if matured_or_soldout == 1
replace sharechange = -L.numberofshare if matured_or_soldout == 1
replace marketvalueusd = 0 if matured_or_soldout== 1

// Additional security details
gen marketpriceusd = marketvalueusd / numberofshare
gen marketvalueusd_l = L.marketvalueusd
gen marketpriceusd_l = L.marketpriceusd
gen numberofshare_l = L.numberofshare
replace numberofshare_l = 0 if missing(numberofshare_l)

// A secondary check of sharechange, using the change in numberofshare
gen d_numberofshare = numberofshare - numberofshare_l
gen growth_sharechange = sharechange/numberofshare_l
gen growth_d_numberofshare = d_numberofshare/numberofshare_l
gen growth_mv = (marketvalueusd-marketvalueusd_l)/marketvalueusd_l
gen sharechange_mns = d_numberofshare if abs(growth_d_numberofshare - growth_mv) < .85 & mns_class=="B" // NB: this has been checked for behavior of missings
replace sharechange_mns = sharechange if missing(sharechange_mns) & abs(growth_sharechange - growth_mv) < .85 & mns_class=="B" 
replace sharechange_mns = d_numberofshare if abs(growth_d_numberofshare - growth_mv) < 1.5 & missing(sharechange_mns) & mns_class=="E"
replace sharechange_mns = sharechange if abs(growth_sharechange - growth_mv) < 1.5 & mns_class=="E"

// Create constant fx series, independently of securities flows.
rename iso_currency_code fund_iso_currency_code
rename currency_id iso_currency_code 
merge m:1 iso_currency_code date_m using "$output/ER_Data/IFS_ERdata.dta", keep(1 3) keepusing(Value)
rename Value lcu_per_usd_SPOT_eop
rename iso_currency_code currency_id 
rename fund_iso_currency_code iso_currency_code
drop _merge
replace lcu_per_usd_SPOT_eop = 1 if currency_id == "USD"
bysort currency_id: gen lcu_constfx_temp=lcu_per_usd_SPOT_eop if date_m==tm(2017m12)
bysort currency_id: egen lcu_constfx=max(lcu_constfx_temp)	
bysort currency_id: gen marketvalueusd_constfx = marketvalueusd * lcu_per_usd_SPOT_eop / lcu_constfx
tsset id date_q
gen timetolast_cfx=date_m-L.date_m
gen marketvalueusd_constfx_l = L.marketvalueusd_constfx
replace marketvalueusd_constfx=0 if missing(marketvalueusd_constfx)
replace marketvalueusd_constfx_l=0 if missing(marketvalueusd_constfx_l)
gen marketvaluechange_constfx = marketvalueusd_constfx-marketvalueusd_constfx_l
gen marketvaluechange_rebasedcfx = marketvalueusd * lcu_per_usd_SPOT_eop / L.lcu_per_usd_SPOT_eop - marketvalueusd_l

// Generate our preferred flow measure, mixture of A,A+B
gen flow_usd = sharechange_mns * marketpriceusd_l
replace flow_usd = marketvaluechange_constfx if missing(flow_usd)

// Flow value, if sharechange_mns is there
gen increase_A = sharechange_mns * marketpriceusd_l if !missing(sharechange_mns)
gen increase_B = marketvaluechange_constfx - increase_A if !missing(increase_A)
gen increase_C = lcu_per_usd_SPOT_eop / L.lcu_per_usd_SPOT_eop * marketvalueusd_l if !missing(increase_A)
gen increase_resid = (marketvalueusd - marketvalueusd_l) - (increase_A + increase_B + increase_C) if !missing(increase_A)
drop numberofshare* sharechange d_numberofshare growth*

// Within versus between
local tol_mpid_ret = .5	
bysort MasterPortfolioId date_q: egen portfolio_value_l = total(marketvalueusd_l)
egen marketvalueusd_pricelast = rowtotal(marketvalueusd_l flow_usd)
bysort MasterPortfolioId date_q: egen portfolio_value = total(marketvalueusd_pricelast)
merge m:1 MasterPortfolioId date_q using "$temp/morningstar_api_data/MPID_netinflow_Q.dta", keep(1 3) nogen
tsset id date_q
gen wgt_in_port_l = marketvalueusd_l / portfolio_value_l
gen wgt_in_port = marketvalueusd_pricelast / portfolio_value
replace wgt_in_port_l = 0 if missing(wgt_in_port_l)
replace wgt_in_port = 0 if missing(wgt_in_port)
gen mpid_net_apprec = ((portfolio_value - portfolio_value_l) - net_inflow)/portfolio_value_l
replace mpid_net_apprec = (portfolio_value - net_inflow)/portfolio_value if missing(portfolio_value_l)
replace net_inflow = . if abs(mpid_net_apprec) > `tol_mpid_ret'
gen flow_usd_bet = wgt_in_port_l * net_inflow if !missing(net_inflow)
gen flow_usd_wit = (wgt_in_port - wgt_in_port_l) * (net_inflow + portfolio_value_l) if !missing(net_inflow)
gen flow_usd_res = flow_usd - (flow_usd_bet + flow_usd_wit) if !missing(net_inflow)
gen flow_usd_mis = flow_usd if missing(net_inflow)

ren marketvalue* mv*
ren marketprice* mp*
save "$reconstruct/HD_part_`part'_panel_Q", replace emptyok
